﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Caifu.Clients;
using Caifu.Fx;

namespace Caifu.WebSite
{

    public partial class Register : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod == "POST")
            {
                PostCount++;
                if (CheckForm())
                {
                    UserClient.Instance.AddUser(new User()
                    {
                        Email = PV("rusername"),
                        Password = MD5Hash.ComputeHash(this.PV("rpwd"))
                    });
                }

                if (PostCount > MAXREQUEST)
                {
                    CallScript("StartVerifyCode(Dom.$('RegVPanel'), Dom.$('RegVCodeImg'), 'reg');");
                }
            }
        }

        /// <summary>
        /// 验证邮箱
        /// <param name="email">邮箱输入控件的名称</param>
        /// </summary>
        /// <returns>ture：通过，false：未通过</returns>
        Boolean verifyEmail(String email)
        {
            Regex rx = new Regex(@"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$");
            if (email.Equals(""))
            {
                //ShowMessage("请输入邮箱地址");
                return false;
            }
            if (email.Length > 50)
            {
                //ShowMessage("邮箱地址不得超过50个字符");
                return false;
            }
            if (!rx.IsMatch(email))
            {
                //ShowMessage("邮箱地址格式不正确");
                return false;
            }
            if (CheckExistByEmail(email))
            {
                //ShowMessage("该邮箱已被注册，请更换其他邮箱");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 登录时验证输入用户密码
        /// </summary>
        /// <returns>ture：通过，false：未通过</returns>
        Boolean verifyPwd(String pwd, String repwd)
        {
            Regex rx = new Regex(@"^[0-9a-zA-Z][0-9a-zA-Z]*?$");
            if (pwd.Equals(""))
            {
                //ShowMessage("密码不能为空");
                return false;
            }
            if (!rx.IsMatch(pwd) || pwd.Length < 6 || pwd.Length > 16)
            {
                //ShowMessage("密码由6-16位字母或数字构成");
                return false;
            }
            if (repwd.Equals(""))
            {
                //ShowMessage("请输入确认密码");
                return false;
            }
            if (!pwd.Equals(repwd))
            {
                //ShowMessage("两次密码输入不一致");
                return false;
            }
            return true;
        }


        /// <summary>
        /// 检查用户邮箱的存在性；
        /// </summary>
        /// <param name="email">用户邮箱</param>
        /// <returns>true:用户存在，false:用户不存在</returns>
        Boolean CheckExistByEmail(String email)
        {
            return UserClient.Instance.CheckExistByEmail(email);
        }

        /// <summary>
        /// 检测用户昵称的存在性;
        /// </summary>
        /// <returns>true:存在冲突，false:不存在冲突</returns>
        Boolean CheckExistByName(String username)
        {
            return UserClient.Instance.CheckExistByName(username);
        }

        /// <summary>
        /// 检测当前表单
        /// </summary>
        /// <returns></returns>
        Boolean CheckForm()
        {
            if (PostCount > MAXREQUEST + 1)
            {
                if (!verifyCode(PV("RegVCode"), "reg"))
                {
                    //ShowMessage("验证码错误或过期");
                    return false;
                }
            }
            return verifyEmail(PV("rusername")) && verifyPwd(PV("rpwd"), PV("rerpwd"));
        }

        /// <summary>
        /// 验证用户输入的验证码信息
        /// </summary>
        /// <param name="inputCode">用户输入的验证码</param>
        /// <returns></returns>
        Boolean verifyCode(String inputCode, String key)
        {
            return String.IsNullOrEmpty(inputCode) ? false
                : (new ValidateCodeManager(this.Context)).ValidationCode(inputCode, key);
        }
    }
}